package com.fc.review;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fc.review.data.Shop;
import com.fc.review.data.ShopReview;

/**
 * Servlet implementation class FCReview
 */
@WebServlet("/FCReview")
public class FCReview extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FCReview() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String method = request.getParameter("method");
		if(method.equals("set")){
			saveReview(request, response);
		}
	}

	private void saveReview(HttpServletRequest request, HttpServletResponse response) {
		String shopName = request.getParameter("shop");
		int rating = Integer.parseInt(request.getParameter("rating"));
		String comment = request.getParameter("comment");
		Shop shop = new Shop();
		shop.setShopName(shopName);
		shop.setShopId(getShopId(shop));
		
		ShopReview review = new ShopReview();
		review.setShop(shop);
		review.setRating(rating);
		review.setComment(comment);
		
		

	}
	public String getShopId(Shop shop)  {
	 return shop.getShopName().substring(0, 4);	
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
